#include "FbProgressBar.h"
#include <gdi.h>
#include <Stringloader.h>


CFbProgressBar* CFbProgressBar::NewLC(CCoeControl& aParent, const TRect &aRect)
{
	CFbProgressBar* self = new(ELeave) CFbProgressBar;
	CleanupStack::PushL(self);
	self->ConstructL(aParent, aRect);
	return self;
}

CFbProgressBar* CFbProgressBar::NewL(CCoeControl& aParent, const TRect &aRect)
{
	CFbProgressBar* self = CFbProgressBar::NewLC(aParent, aRect);
	CleanupStack::Pop();
	return self;
}

CFbProgressBar::~CFbProgressBar()
{
	delete iContentText;
	iContentText = NULL;
}

void CFbProgressBar::ConstructL(CCoeControl& aParent, const TRect& aRect /*, const TDesC &aContentTxt */)
{
	m_style = EStylePercent;
	SetContainerWindowL(aParent);
	//CreateWindowL();
	iContentTextColor = KRgbWhite;
	iContentText = HBufC::New(((TDesC)KFbProgressBarDefaultText).Length());//the length 100 need change! <update 2009'1.15>
	iContentText->Des().Copy(KFbProgressBarDefaultText);
	//TRect tempRect(20, 320-75, 240-20, 320-60);
	iProgress_Rect = aRect; //KFbProgressBarDefaultRect,tempRect;
	
	//Set default running step and data length!
	iTotalLength = 100;
	iRunningSteps = 0;
	
	ChangeDrawRect();

	SetRect(drawRect);

	ActivateL();
}

void CFbProgressBar::Draw(const TRect& /*aRect*/) const
{
	CWindowGc& gc = SystemGc ();

	//Draw ProgressBar	<BEGIN>
	TInt iRunCount = ((iProgress_Rect.iBr.iX - iProgress_Rect.iTl.iX -4)*iRunningSteps/iTotalLength) + iProgress_Rect.iTl.iX +2;//MAX_LOGORUNTIMER
	TInt iRunPercent = iRunningSteps * 100 /iTotalLength;
	TRect progressRun_Rect(iProgress_Rect.iTl.iX +2,iProgress_Rect.iTl.iY+2,
			iRunCount,iProgress_Rect.iBr.iY-2);
	
	gc.SetPenStyle (CGraphicsContext::ESolidPen );
	gc.SetPenColor(KRgbBlack);
	gc.SetBrushColor (KRgbWhite);
	gc.SetBrushStyle (CGraphicsContext::ESolidBrush);
	gc.DrawRect (iProgress_Rect);//Draw null rect!
	gc.SetBrushStyle (CGraphicsContext::ESolidBrush);
	gc.SetBrushColor (KRgbDarkBlue);
	gc.DrawRect (progressRun_Rect);//Draw running rect!
	
	//draw complete percent text!
	CFont* font = NULL;//iCoeEnv->NormalFont();
	TFontSpec fontsp = iCoeEnv->NormalFont()->FontSpecInTwips();
	TInt font_oHeight = fontsp.iHeight;
	fontsp.iHeight = 7 * font_oHeight / 10;
	iCoeEnv->ScreenDevice ()->GetNearestFontInTwips (font, fontsp );
	if(font)
		gc.UseFont(font);
	else
		gc.UseFont(iCoeEnv->NormalFont());

	if(m_style == EStylePercent)
	{
		gc.SetPenColor(TRgb(0x0f, 0xf0, 0xf0));
		TBuf<12> str_percent;
		str_percent.Format(_L("%d"),iRunPercent);
		str_percent.Append('%');	
		TInt posX = progressRun_Rect.iBr.iX >= 50 ? progressRun_Rect.iBr.iX-27 : 25;
		gc.DrawText(str_percent,TPoint(posX, progressRun_Rect.iBr.iY + 2));
	}
	
	//Draw Text <BEGIN>
	if(iContentText)
	{
		gc.SetPenColor(iContentTextColor);
		gc.SetBrushStyle (CGraphicsContext::ENullBrush);
		TRect txtBox(iProgress_Rect.iTl.iX,
				iProgress_Rect.iTl.iY-25,
				iProgress_Rect.iBr.iX,
				iProgress_Rect.iTl.iY);	
		gc.DrawText(iContentText->Des(),txtBox,txtBox.Height()-5,CGraphicsContext::ECenter);
	}
	
	gc.DiscardFont();
	iCoeEnv->ScreenDevice()->ReleaseFont(font);
	//Draw Text	<END>	
}

CCoeControl* CFbProgressBar::ComponentControl(TInt aIndex) const
{
	return NULL;//iLabelText;
}

TInt CFbProgressBar::CountComponentControls() const
{
	return 0;//目前没有控件返回0.
}

void CFbProgressBar::SizeChanged()
{
	//ChangeDrawRect();
}

//The ProgressBar and Text For User Set!
void CFbProgressBar::SetProgressBarRect(TRect &aRect)
{
	iProgress_Rect = aRect;
	ChangeDrawRect();
	SetRect(drawRect);
	DrawNow();
}

void CFbProgressBar::SetProgressBarColor(TRgb &aColor)
{
	iProgressBar_color = aColor;
}

void CFbProgressBar::SetShowTipText(const TDesC &aText, const TRgb aTextColor)
{
	iContentTextColor = aTextColor;
	
	if(iContentText)
	{
		delete iContentText;
		iContentText = NULL;
	}
	if(aText != KNullDesC)
	{
		iContentText = HBufC::New(aText.Length());
		iContentText->Des().Copy(aText);
	}
	else
	{
		iContentText = HBufC::New(((TDesC)KFbProgressBarDefaultText).Length());//the length 100 need change! <update 2009'1.15>
		iContentText->Des().Copy(KFbProgressBarDefaultText);
	}
}


//set prompt text by resid
void CFbProgressBar::SetShowTipTextL(const TInt aResId, const TRgb aTextColor)
{
	iContentTextColor = aTextColor;
		
	if (iContentText)
	{
		delete iContentText;
		iContentText = NULL;
	}
	
	if(aResId > 0)
	{
		iContentText = StringLoader::LoadL(aResId);
	}
}
	

void CFbProgressBar::ChangeDrawRect()
{
	drawRect.iTl.iX = iProgress_Rect.iTl.iX > 20 ? iProgress_Rect.iTl.iX - 20 : 0;
	drawRect.iTl.iY = iProgress_Rect.iTl.iY > 60 ? iProgress_Rect.iTl.iY - 60 : 0;
	drawRect.iBr.iX = iProgress_Rect.iBr.iX < 220 ? iProgress_Rect.iBr.iX + 20 : 240;
	drawRect.iBr.iY = iProgress_Rect.iBr.iY < 310 ? iProgress_Rect.iBr.iY+ 10 : 320;
}

void CFbProgressBar::SetTotalDataLength(TInt alen)
{
	iTotalLength = alen;
}

void CFbProgressBar::UpdateRunningStep(TInt aRunStep)
{
	//iRunningSteps = iRunningSteps >= iTotalLength ? iTotalLength : aRunStep;
	iRunningSteps += aRunStep;
	if(iRunningSteps >= iTotalLength)
	{
		if(m_style == EStylePercent)
			iRunningSteps = iTotalLength;
		else
			iRunningSteps = 0;
	}
	DrawNow();
}

void CFbProgressBar::SetProgressBarStyle(const TProgressStyle &aStyle)
{
	m_style = aStyle;
}
